<?php

class Admin_AdminController extends Ac_AdminController
{
    public function indexAction()
    {
        //find valid admin controller
        $adminct = glob(APPLICATION_PATH.'/modules/*/controllers/AdminController.php');
        $left = $right = $menu = array();
        $test = true;
        foreach ($adminct as $file) {
            
            preg_match('#'.str_replace('\\','\\\\', APPLICATION_PATH).'/modules/([a-zA-Z]*?)/controllers/AdminController.php#', $file, $match);
            if ($match[1] == 'admin') {
                continue;
            }
            
            include_once($file);
            
            $methods = get_class_methods(ucfirst($match[1]).'_AdminController');
            if (in_array('dashboardAction', $methods)) {
                    $modules[] = (object) array('name' => $match[1]);
            }
            
            //load the menu if needed
            $vars = get_class_vars(ucfirst($match[1]).'_AdminController');            
            if (in_array('menuAction', $methods)) {
                $menu[] = (object) array('module' => $match[1]);
            }
        }
        $this->view->modules = $modules;
        $this->view->menu    = $menu;
        // action body
       // echo $this->render('index');
    }
    
    public function mainmenuAction() {
 //find valid admin controller
        $adminct = glob(APPLICATION_PATH.'/modules/*/controllers/AdminController.php');
        $menu = array();
        $test = true;
        $req  = Zend_Registry::get('request_ori');
        $this->view->current_module = $req->getModuleName();
        foreach ($adminct as $file) {
            preg_match('#'.str_replace('\\','\\\\', APPLICATION_PATH).'/modules/([a-zA-Z]*?)/controllers/AdminController.php#', $file, $match);
            include_once($file);
            $methods = get_class_methods(ucfirst($match[1]).'_AdminController');
            //load the menu if needed
            if (in_array('menuAction', $methods)) {
                $menu[] = (object) array('module' => $match[1]);
            }
        }
        $this->view->menu = $menu;     
        $this->render('mainmenu','mainmenu');
    }
    
    public function extractDashboardAction()
    {
    	$period = 'ALL TIME';
		if(!empty($_GET['from']) && !empty($_GET['to']))
		{
			$from = new MongoDate (strtotime($_GET['from']." 00:00:01"));
			$to = new MongoDate (strtotime($_GET['to']." 23:59:59"));
			//echo $from."-".$to;
			$period = $_GET['from']. ' - ' .$_GET['to'];
			//USER
			$users = Model_User::all(array('c_dt' => array('$gte' => $from, '$lte' => $to)));
			$num_active_user = Model_User::all(array('sa' => array('$exists' => 0), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
        	$num_banned_user = Model_User::all(array('sa' => Model_User::STATUS_BANNED, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
			//language stat
	        $num_en_user = Model_User::all(array('$or' => array(array('la' => array('$exists' => false)), array('la' => 'en')), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
	        $num_tc_user = Model_User::all(array('la' => 'tc', 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();	        
			//newsletter stat
	        $num_newsletter_subs = Model_User::all(array('nl' => 1, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
	        $num_newsletter_nonsubs = Model_User::all(array('$or' => array(array('nl' => array('$exists' => false)), array('nl' => '0')), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();	        
	        //personal accounts	
	        $num_personal_account = Model_User::all(array('cid' => array('$exists' => false), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();	        
	        //biz accounts	
	        $num_business_account = Model_User::all(array('cid' => array('$exists' => true), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
	        //user with ads
	        $num_ad_poster = Model_Listing::distinct('us', array('c_dt' => array('$gte' => $from, '$lte' => $to)));
	        
	        //LISTING
	        //total ads
			$num_all_listing      		= Model_Listing::all(array('c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
			//published ads
        	$num_published_listing      = Model_Listing::all(array('sa' => Model_Listing::STATUS_PUBLISHED, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
        	//unpublished ads
			$num_unpublished_listing    = Model_Listing::all(array('sa' => Model_Listing::STATUS_DRAFT, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
			//deleted ads
	        $num_deleted_listing        = Model_Listing::all(array('sa' => Model_Listing::STATUS_DELETED, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
	        //sold ads
	        $num_sold_listing           = Model_Listing::all(array('sa' => Model_Listing::STATUS_MARK_SOLD, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
        	//organic ads
			$num_organic_listing    	= Model_Listing::all(array('$or' => array(array('sa' => Model_Listing::STATUS_PUBLISHED), array('sa' => Model_Listing::STATUS_MARK_SOLD)), 'pv' => array('$exists' => false), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
			//non-organic ads
			$num_nonorganic_listing    	= Model_Listing::all(array('$or' => array(array('sa' => Model_Listing::STATUS_PUBLISHED), array('sa' => Model_Listing::STATUS_MARK_SOLD)), 'pv' => array('$exists' => true), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
			
			//MESSAGE
			$num_active_message = Model_Message::all(array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
        	$num_users = Model_Message::distinct('fo', array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER, 'c_dt' => array('$gte' => $from, '$lte' => $to)));
        	
        	//COMPANY
        	$num_active_company = Model_Company::all(array('st' => Model_Company::STATUS_ACTIVE, 'cn' => array('$exists' => true), 'bc' => array('$exists' => true), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
	        $num_banned_company = Model_Company::all(array('st' => Model_Company::STATUS_DELETED, 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
	        //pending biz w/o company name or biz cat
	        $num_pending_company = Model_Company::all(array('$or' => array(array('cn' => array('$exists' => false)), array('bc' => array('$exists' => false))), 'c_dt' => array('$gte' => $from, '$lte' => $to)))->count();
		}else
		{
			//USER
			$users = Model_User::all();
			$num_active_user = Model_User::all(array('sa' => array('$exists' => 0)))->count();
        	$num_banned_user = Model_User::all(array('sa' => Model_User::STATUS_BANNED))->count();
			//language stat
	        $num_en_user = Model_User::all(array('$or' => array(array('la' => array('$exists' => false)), array('la' => 'en'))))->count();
	        $num_tc_user = Model_User::all(array('$or' => array(array('la' => 'tc'))))->count();
	        //newsletter stat
	        $num_newsletter_subs = Model_User::all(array('nl' => 1))->count();
	        $num_newsletter_nonsubs = Model_User::all(array('$or' => array(array('nl' => array('$exists' => false)), array('nl' => '0'))))->count();
	        //personal accounts
	        $num_personal_account = Model_User::all(array('cid' => array('$exists' => false)))->count();
	        //biz accounts	
	        $num_business_account = Model_User::all(array('cid' => array('$exists' => true)))->count();
	        //user with ads
	        $num_ad_poster = Model_Listing::distinct('us');
	        
	        //LISTING
	        //total ads
			$num_all_listing      		= Model_Listing::all()->count();
			//published ads
        	$num_published_listing      = Model_Listing::all(array('sa' => Model_Listing::STATUS_PUBLISHED))->count();
        	//unpublished ads
			$num_unpublished_listing    = Model_Listing::all(array('sa' => Model_Listing::STATUS_DRAFT))->count();
			//deleted ads
	        $num_deleted_listing        = Model_Listing::all(array('sa' => Model_Listing::STATUS_DELETED))->count();
	        //sold ads
	        $num_sold_listing           = Model_Listing::all(array('sa' => Model_Listing::STATUS_MARK_SOLD))->count();
			//organic ads
			$num_organic_listing    	= Model_Listing::all(array('$or' => array(array('sa' => Model_Listing::STATUS_PUBLISHED), array('sa' => Model_Listing::STATUS_MARK_SOLD)), 'pv' => array('$exists' => false)))->count();
			//non-organic ads
			$num_nonorganic_listing    	= Model_Listing::all(array('$or' => array(array('sa' => Model_Listing::STATUS_PUBLISHED), array('sa' => Model_Listing::STATUS_MARK_SOLD)), 'pv' => array('$exists' => true)))->count();
			
			//MESSAGE
			$num_active_message = Model_Message::all(array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER))->count();
			$num_users = Model_Message::distinct('fo', array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER));
			
			//COMPANY
			$num_active_company = Model_Company::all(array('st' => Model_Company::STATUS_ACTIVE, 'cn' => array('$exists' => true), 'bc' => array('$exists' => true)))->count();
	        $num_banned_company = Model_Company::all(array('st' => Model_Company::STATUS_DELETED))->count();
	        //pending biz w/o company name or biz cat
	        $num_pending_company = Model_Company::all(array('$or' => array(array('cn' => array('$exists' => false)), array('bc' => array('$exists' => false)))))->count();
		}
		
        $count = $count2 = 0;
        foreach($users as $user)
		{
			$listing = '';
			$listing = Model_Listing::all(array('us' => $user->_id))->count();
			if($listing == 0)
			{
				$count++;
			}
			
			if($listing >= 100)
			{
				$count2++;
			}
		}
        $num_non_poster = $count;
        $num_powerposter = $count2;
        
        $content = '<table><tr><th>Dashboard Report</th><th></th></tr>';
        $content .= '<tr><td>Report Period</td><td>'.$period.'</td></tr>';
        $content .= '<tr><td></td><td></td></tr>';
        
        $content .= '<tr><th>Listing</th></tr>';
        $content .= '<tr><td>Number of Listing</td><td>'.$num_all_listing.'</td></tr>';
        $content .= '<tr><td>Number of published Listing</td><td>'.$num_published_listing.'</td></tr>';
        $content .= '<tr><td>Number of unpublished Listing</td><td>'.$num_unpublished_listing.'</td></tr>';
        $content .= '<tr><td>Number of deleted Listing</td><td>'.$num_deleted_listing.'</td></tr>';
        $content .= '<tr><td>Number of marked as sold Listing</td><td>'.$num_sold_listing.'</td></tr>';
        $content .= '<tr><td>Number of Organic Listing</td><td>'.$num_organic_listing.'</td></tr>';
        $content .= '<tr><td>Number of Non-Organic Listing</td><td>'.$num_nonorganic_listing.'</td></tr>';
        $content .= '<tr><td></td><td></td></tr>';
        
        $content .= '<tr><th>User</th></tr>';
        $content .= '<tr><td>Number of active User</td><td>'.$num_active_user.'</td></tr>';
        $content .= '<tr><td>Number of banned User</td><td>'.$num_banned_user.'</td></tr>';
        $content .= '<tr><td>Number of Personal account</td><td>'.$num_personal_account.'</td></tr>';
        $content .= '<tr><td>Number of Business account</td><td>'.$num_business_account.'</td></tr>';
        $content .= '<tr><td>Number of Eng User</td><td>'.$num_en_user.'</td></tr>';
        $content .= '<tr><td>Number of TC User</td><td>'.$num_tc_user.'</td></tr>';
        $content .= '<tr><td>Number of Newsletter Subscriber</td><td>'.$num_newsletter_subs.'</td></tr>';
        $content .= '<tr><td>Number of Non Newsletter Subscriber</td><td>'.$num_newsletter_nonsubs.'</td></tr>';
        $content .= '<tr><td>Number of Poster</td><td>'.count($num_ad_poster).'</td></tr>';
        $content .= '<tr><td>Number of Non-Poster</td><td>'.$num_non_poster.'</td></tr>';
        $content .= '<tr><td>Number of Power Poster</td><td>'.$num_powerposter.'</td></tr>';
        
        $content .= '<tr><th>Company</th></tr>';
        $content .= '<tr><td>Number of Active Company</td><td>'.$num_active_company.'</td></tr>';
        $content .= '<tr><td>Number of Banned Company</td><td>'.$num_banned_company.'</td></tr>';
        $content .= '<tr><td>Number of Pending Company</td><td>'.$num_pending_company.'</td></tr>';
        
        $content .= '<tr><th>Message</th></tr>';
        $content .= '<tr><td>Number of Message</td><td>'.$num_active_message.'</td></tr>';
        $content .= '<tr><td>Number of Message Sender</td><td>'.count($num_users).'</td></tr>';
        
        $content .= "</table>";
		$filename = "myExcelFile.xls";
        header ("Content-type: application/octet-stream");
		header ("Content-Disposition: attachment; filename=".$filename);
        
        echo $content;
        exit;
	}

}

